package com.smec.moblie.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.smec.moblie.R;
import com.smec.moblie.bean.MaintenancePhotoBean;
import com.smec.moblie.common.ConstValue;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by sunyubo on 16/3/9.
 */
public class ScenePhotoAdapter extends BaseAdapter implements ConstValue{
    private ArrayList<MaintenancePhotoBean> maintenancePhotoBeanSceneList;
    private Context context;
    private LayoutInflater inflater;
    private String imgPath;
    private DisplayImageOptions options;

    public ScenePhotoAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        imgPath = Environment.getExternalStorageDirectory() + "/DCIM/Camera/";

        options = new DisplayImageOptions.Builder().showStubImage(R.mipmap.photo).showImageForEmptyUri(R.mipmap.photo).showImageOnFail(R.mipmap.photo)
                .cacheInMemory(true).cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
                .build();
        maintenancePhotoBeanSceneList = new ArrayList<MaintenancePhotoBean>();
    }

    public void setList(ArrayList<MaintenancePhotoBean> maintenancePhotoBeanSceneList) {
        this.maintenancePhotoBeanSceneList = maintenancePhotoBeanSceneList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return maintenancePhotoBeanSceneList.size();
    }

    @Override
    public Object getItem(int position) {
        return maintenancePhotoBeanSceneList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (!maintenancePhotoBeanSceneList.get(position).getResource_id().equals("")) {
            convertView = inflater.inflate(R.layout.item_scenephoto, null);

            ImageView imageView_scenePhoto = (ImageView) convertView.findViewById(R.id.imageView_scenePhoto);

            ImageLoader imageLoader = ImageLoader.getInstance();

            File file1 = new File(imgPath + maintenancePhotoBeanSceneList.get(position).getResource_id() + ".jpeg");
            if (file1.exists()) {
                imageLoader.displayImage("file://" + imgPath + maintenancePhotoBeanSceneList.get(position).getResource_id() + ".jpeg", imageView_scenePhoto, options);
            } else {
                imageLoader.displayImage(SERVER_BASE_PATH + "rest/resources/" + maintenancePhotoBeanSceneList.get(position).getResource_id(), imageView_scenePhoto, options);
            }
        }
        return convertView;
    }

}
